﻿function Init()
{
	if (typeof(SetupIme) != "undefined")
		SetupIme();

	//键盘处理“上一页”，“下一页”
	$("body").keyup(function(e)
	{
		if (e.target.tagName == "INPUT" || e.target.tagName == "TEXTAREA")
			return;

		if (e.keyCode == 37)//上一页
		{
			if ($("#PrevPage").length > 0)
				window.location = $("#PrevPage").attr("href");
		}
		else if (e.keyCode == 39)//下一页
		{
			if ($("#NextPage").length > 0)
				window.location = $("#NextPage").attr("href");
		}
	});
	
	if (_page == "index")
	{
		$("input[name=q]").focus();
		$("h1 a").click(function()
		{
			var me = $(this);
			if (this.href == "")
				return false;
			
			me.siblings().each(function(){$(this).attr("href", "#");});
			me.removeAttr("href");
			return false;
		});
		
		$("form").submit(function()
		{
			if (this.q.value == "")
				return false;
		
			var i = 0;
			$("h1 a").each(function()
			{
				if (this.href == "")
					return false;
				i++;
			});
			
			switch (i)
			{
				case 0:
					this.action = _web;
					break;
				case 1:
					this.action = _image;
					break;
				case 2:
					this.action = _music;
					break;
				case 3:
					this.action = _video;
					break;
			}
			
			return true;
		});
	
		$("[name=q]").keyup(function(e)
		{
			if (this.value == "" || e.keyCode == 40 || e.keyCode == 38 || e.keyCode == 37 || e.keyCode == 39)
				return;
			
			var q = $(this);
			$.ajax(
			{
				url : "ajax_keyword.aspx?q=" + encodeURIComponent(this.value),
				type : "GET",
				success : function(data)
				{
					if ($.trim(data) == "")
					{
						$("#DropDown").hide();
						return;
					}
					
					var dd = $("#DropDown");
					dd.css({top:q.offset().top + q.innerHeight(),left:q.offset().left,width:q.width()});
					dd.html(data).show();	
				}
			});
		}).keydown(function(e)
		{
			if (e.keyCode == 40)//down arrow
			{
				var dd = $("#DropDown");
				if (dd.is(":visible"))
				{
					var lis = dd.children();
					var sel = dd.find(".Selected");
					sel.removeClass("Selected");
					if (sel.length > 0)
					{
						var index = lis.index(sel);
						if (index + 1 < lis.length)
							lis.eq(index + 1).addClass("Selected");
						else if (index != 0 && index != lis.length - 1)
							lis.first().addClass("Selected");
					}
					else
					{
						lis.first().addClass("Selected");
					}
				}
				else
				{
					dd.show();
				}
				e.preventDefault();
			}
			else if (e.keyCode == 38)//up arrow
			{
				var dd = $("#DropDown");
				if (dd.is(":visible"))
				{
					var lis = dd.children();
					var sel = dd.find(".Selected");
					sel.removeClass("Selected");
					if (sel.length > 0)
					{
						var index = lis.index(sel);
						if (index > 0)
							lis.eq(index - 1).addClass("Selected");
					}
					else
					{
						lis.last().addClass("Selected");
					}
				}
				else
				{
					dd.show();
				}
				e.preventDefault();
			}
			else if (e.keyCode == 13)//enter
			{
				var dd = $("#DropDown");
				if (dd.is(":visible") && dd.children(".Selected").length > 0)
				{
					$("[name=q]").val(dd.children(".Selected").text());
				}
			}
		}).blur(function()
		{
			$("#DropDown").hide();
		});

		//-----------------------------------------
		//Start : 添加入站
		var referrer = document.referrer;
		if (referrer != null && referrer != "" && referrer.indexOf("http://" + document.domain) != 0)
			$.post(_webPath + "alicms.aspx?Mark=Link&Object=Link&Operation=IncrementIn&Referrer=" + encodeURIComponent(referrer), {});
		//End : 添加入站
		//-----------------------------------------		
	}
	else if (_page == "web" || _page == "image" || _page == "music" || _page == "video")
	{
		$.post(_webPath + "alicms.aspx?Object=Keyword&Operation=IncrementHitCount&Name=" + encodeURIComponent($("[name=q]").val()), {});

		$("#RecordCount1").html($("#RecordCount2").html());
		
		if (_page == "image" || _page == "video")
			$("ul:odd").addClass("Odd");
		
		if (_page == "music")
		{
			$("th a,td a").click(function()
			{
				var player = $("#Player");
				if (player.length == 0)
				{
					$("tbody").append("<tr id=\"Player\" style=\"display:none\"><td colspan=\"7\"><iframe width=\"600\" frameborder=\"0\"></iframe></td></tr>");
					player = $("#Player");
				}
				
				var tr = $(this).parents("tr");
				var type = tr.children().eq(4).html();
				var url = tr.children().eq(6).children("a").attr("href");
				player.find("iframe").attr("src", url);
				player.insertAfter(tr);
				player.show();
				return false;
			});
		}
	}
}

Init();